home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10658 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  95 lines

  1. Path: demigod.rutgers.edu!not-for-mail
  2. From: alii@demigod.rutgers.edu (Syed Ali)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie question about function calling.
  5. Date: 8 Mar 1996 22:24:17 -0500
  6. Organization: Rutgers University LCSR
  7. Message-ID: <4hqtl1$5os@demigod.rutgers.edu>
  8. References: <4hohd3$r9@brickbat.mindspring.com>
  9. NNTP-Posting-Host: demigod.rutgers.edu
  10.  
  11.  
  12. Although i do not see why your program is locking up, try the
  13. approach below.  It uses less memory and should work.
  14.  
  15. Why don't you change it so that the filename is a pointer to
  16. a string and not an array of characters? Although they sound
  17. the same, they are represented  differently.
  18.  
  19. You would therfore have in getinfo():
  20. char * infile;
  21. scanf("%s",infile);
  22. return(infile);
  23.  
  24. Change counter to:
  25. int counter (char * filename);
  26.  
  27. And you can then print it as:
  28. printf("%s",filename);
  29.  
  30. Regards,
  31. Syed Ali
  32.  
  33. ------------------------------------------------------------------
  34. wdnick@mindspring.com (William "Doug" Nicholson) writes:
  35.  
  36. >I'm having a problem somewhere between the following two functions.
  37. >My main calls the function 'counter' (shown below) which in turn calls
  38. >'getinfo' by trying to assign 'getinfo's return value to the string
  39. >variable 'filename'.  'Getinfo' prompts the user to enter a filename
  40. >and stores it in the string 'infile'.  I've added a couple of lines
  41. >below and started them in my post with the > so they will stand out.
  42. >In the first one (in the getinfo function), the printf line prints
  43. >whatever I type in at the prompt.  Theoretically this should be passed
  44. >to 'filename' in the other function.  Well, the printf that tries to
  45. >print "FILE = 'filename'" stops after "FILE =" and the computer locks
  46. >up.
  47.  
  48. >Anybody got any idea what's happening here?  Have I overlooked a
  49. >really stupid thing or what?
  50.  
  51. >Thanks for any help.  The problem area is listed below.
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. >char getinfo(void)
  60. >/*****************************************************************
  61. > * This function prompts the user for a filename and returns it. *
  62. > *****************************************************************/
  63. >{
  64. >    char    infile[15];    /* file to process        */
  65.  
  66. >    printf("Enter the path and filename of a file to process ");
  67. >    printf("or enter XX to quit >");
  68. >    scanf("%s", infile);
  69.  
  70. >>    printf("FILE = %s\n\n", infile);
  71.  
  72. >    return(infile[15]);
  73. >}                /* end function getinfo        */
  74.  
  75. >int counter(void)
  76. >/*****************************************************************
  77. > * This function counts the number of times that each character  *
  78. > * is used in a file and returns the count.                      *
  79. > *****************************************************************/
  80. >{
  81. >    int    i,        /* lengths of triangle sides    */
  82. >        count[128];    /* character count array    */
  83.  
  84. >    char    chr,
  85. >        filename[15] = "",
  86. >        foo[2] = "1";
  87.  
  88. >    FILE    *inp,        /* input file pointer        */
  89. >        *outp;        /* output file pointer        */
  90.  
  91. >    filename[15] = getinfo();
  92.  
  93. >>    printf("FILE = %s\n\n", filename);
  94.  
  95.